package com.yonyou.crm.sys.appmenu.service;

import java.util.*;

import org.springframework.stereotype.Service;
import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.sys.appmenu.entity.*;
import com.yonyou.iuap.context.InvocationInfoProxy;

@Service
public class AppMenuBizServiceImpl extends BaseBizServiceImpl<MenuVO>{
	
	/**
	 * 列表
	 * @param page
	 * @param paramMap
	 * @return
	 */
	public Page<MenuVO> getList(Page<MenuVO> page,Map<String, Object> paramMap) {
		
		Page<MenuVO> menuPage = null;
		if (paramMap != null) {
			paramMap.put("tenantId", new Long(InvocationInfoProxy.getTenantid()));
			menuPage = super.pageSelect(MenuVO.class, page, paramMap.keySet().toArray(new String[0]), paramMap.values().toArray(new Object[0]));
		}
		else {
			menuPage = super.pageSelect(MenuVO.class, page, null, null);
		}
		
		return menuPage;
	}
	
	/**
	 * App树状结构
	 * @param paramMap
	 * @return
	 */
	public List<AppMenuVO> getList(Map<String, Object> paramMap){
		
		List<MenuVO> menuList = new ArrayList<MenuVO>();
		if (paramMap == null) {
			paramMap = new HashMap<String, Object>();
		}
		paramMap.put("tenantId", new Long(InvocationInfoProxy.getTenantid()));
		menuList = dao.selectVoList(MenuVO.class, "getAppMenuList", paramMap);
		//当前集团或公司没有菜单时获取预置菜单
		if (menuList.size() <= 0) {
			paramMap.put("tenantId", 0);
			menuList = dao.selectVoList(MenuVO.class, "getAppMenuList", paramMap);
		}
		
		List<AppMenuVO> res = transAppMenu(menuList);
		return res;
	}
	
	/**
	 * 转换菜单格式
	 * @param menuList
	 * @return
	 */
	public List<AppMenuVO> transAppMenu(List<MenuVO> menuList) {
		List<AppMenuVO> res = new ArrayList<AppMenuVO>();
		for(int i = 0; i < menuList.size(); i++) {
			MenuVO menu = menuList.get(i);
			AppMenuVO appMenu = new AppMenuVO();
			appMenu.setObjid(String.valueOf(menu.getObjId()));
			appMenu.setName(menu.getName());
			appMenu.setFuncode(menu.getFuncode());
			appMenu.setPosition(menu.getPosition());
			appMenu.setIsfastrecord((menu.getIsFastrecord() == 1 )? "Y" : "N");
			appMenu.setIsfastadd((menu.getIsFastadd() == 1 )? "Y" : "N");
			List<MenuVO> nextList = menu.getNextlist();
			List<AppMenuItemVO> appNextList = transAppChildMenu(nextList);
			appMenu.setNextlist(appNextList);
			res.add(appMenu);
		}
		return res;
	}
	
	/**
	 * 转换子菜单格式
	 * @param nextList
	 * @return
	 */
	public List<AppMenuItemVO> transAppChildMenu(List<MenuVO> nextList) {
		List<AppMenuItemVO> appNextList = new ArrayList<>();
		if ((nextList != null) && (nextList.size() > 0)){
			for (int j = 0; j < nextList.size(); j++ ) {
				MenuVO menuItem = nextList.get(j);
				AppMenuItemVO appMenuItem = new AppMenuItemVO();
				appMenuItem.setObjid(String.valueOf(menuItem.getObjId()));
				appMenuItem.setName(menuItem.getName());
				appMenuItem.setFuncode(menuItem.getFuncode());
				appMenuItem.setIsfastrecord((menuItem.getIsFastrecord() == 1) ? "Y" : "N");
				appMenuItem.setIsfastadd((menuItem.getIsFastadd() == 1) ? "Y" : "N");
				appNextList.add(appMenuItem);
			}
		}
		return appNextList;
	}
	
	/**
	 * 详情
	 * @param id
	 * @return
	 */
	public MenuVO getDetail(Long id) {
		MenuVO newMenu = super.getVOById(MenuVO.class, id);
		return newMenu;
	}
	
	@Override
	protected void beforeSaveVO(MenuVO vo) {
		
		super.beforeSaveVO(vo);

		if(vo.getOrgId() == null) {
			String orgId = InvocationInfoProxy.getParameter("orgid");
			vo.setOrgId(Long.parseLong(orgId));
		}

		Long loginUserID =  new Long(InvocationInfoProxy.getParameter("id"));
		Date date = new Date();
		
		vo.setTenantId(new Long(InvocationInfoProxy.getTenantid()));
		
		vo.setEnableTime(date);
		vo.setEnableUserId(loginUserID);
		
		vo.setSysCreatedTime(date);
		vo.setSysCreatedUserId(loginUserID);
		
		vo.setSysModifiedTime(date);
		vo.setSysModifiedUserId(loginUserID);
	}
	
	/**
	 * 编辑
	 * @param menu
	 * @return
	 */
	public MenuVO update(MenuVO menu) {
		return super.updateVO(menu);
	}
	
	@Override
	protected void beforeUpdateVO(MenuVO vo) {
		
		super.beforeUpdateVO(vo);

		vo.setSysModifiedTime(new Date());
		vo.setSysModifiedUserId(new Long(InvocationInfoProxy.getParameter("id")));
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	public int delete(Long id) {
		MenuVO menu = new MenuVO();
		menu.setId(id);
		return super.deleteVO(menu);
	}
	
	/**
	 * 批量删除
	 * @param ids
	 */
	public void batchDelete(String[] ids) {
		super.batchDeleteByIDs(MenuVO.class, ids);
	}
	
	@Override
	protected void beforeDeleteVO(MenuVO vo) {
		
		super.beforeDeleteVO(vo);
				
		vo.setDeletedTime(new Date());
		vo.setDeletedUserId(new Long(InvocationInfoProxy.getParameter("id")));	
	}
	
	/**
 	 * 更新启用状态
	 * @param id
	 * @param enableState
	 * @return
	 */
	public MenuVO updateEnableState(Long id, Integer enableState) {
		
		MenuVO menu = new MenuVO();
		menu.setId(id);
		menu.setEnableState(enableState);
		menu.setEnableTime(new Date());
		menu.setEnableUserId(new Long(InvocationInfoProxy.getParameter("id")));
		return super.updateVO(menu);
	}
	
	@Override
	protected void afterSaveVO(MenuVO vo) {
		super.afterSaveVO(vo);
	}
	
	@Override
	protected void afterUpdateVO(MenuVO vo) {
		super.afterUpdateVO(vo);
	}
	
	@Override
	protected void afterDeleteVO(MenuVO vo) {
		super.afterDeleteVO(vo);
	}

}	
